var CMS = {};
CMS.Front = {};
CMS.Front.Index = {};
CMS.Front.Index.initSlider = function(_slider){
	var options = arguments[1];
	var _container = arguments[2];
	var container = $("#"+_container);
	var win = $(window);
	
	if(options===undefined){
		options={auto:false};
	}
	var sudoSlider = $("#"+_slider).sudoSlider(options);
	
	if(container!==undefined){
		win.on("resize blur focus", function () {
            var height = container.height();
            var width = container.width();
            sudoSlider.height(height);
            container.height(height);
            
            sudoSlider.width(width);
            //container.width(width);
        }).resize();
	}
	
	sudoSlider.find(".slide").each(function () {
        var slide = $(this);
        var imageSrc = slide.attr("data-background");
        if (!imageSrc) {
            return;
        }
        $("<img />").attr("src", imageSrc).properload(function () {
            var backgroundImage = $(this);
            var imageHeight = backgroundImage[0].naturalHeight;
            var imageWidth = backgroundImage[0].naturalWidth;

            if (!imageHeight) {
                var img = new Image();
                img.src = imageSrc;
                imageWidth = img.width;
                imageHeight = img.height;

            }

            var aspectRatio = imageWidth / imageHeight;

            backgroundImage.appendTo(slide);

            slide.css({
                zIndex: 0
            });

            backgroundImage.css({
                position: "absolute",
                zIndex: -1,
                top:0,
                left: 0
            });

            win.on("resize blur focus", function () {
                var sliderWidth = sudoSlider.width();
                var sliderHeight = sudoSlider.height();
                if ((sliderWidth / sliderHeight) < aspectRatio ) {
                    var leftMargin = ((sliderWidth - (sliderHeight * aspectRatio)) / 2) + "px";
                    backgroundImage.css({
                        top: 0,
                        left: leftMargin,
                        width: sliderHeight * aspectRatio,
                        height: sliderHeight
                    });
                } else {
                    backgroundImage.css({
                        left: 0,
                        //top: ((sliderHeight - (sliderWidth / aspectRatio)) / 2) + "px",
                        //height: sliderWidth / aspectRatio,
                        height:sliderHeight,
                        width: sliderWidth
                    });
                }
            }).resize();

        }, true);
    });
};
